//
//  ZMTabBarController.swift
//  LoveBeen
//
//  Created by pg on 2017/8/6.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMTabBarController: UITabBarController  {

    fileprivate lazy var custabBar: ZMTabBar = {
       let tab = ZMTabBar()
        return tab
    }()
    
    var indexFlag = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setValue(custabBar, forKey: "tabBar")
      // self.tabBar.delegate = self
        
        
        //添加自控制器
         addChildViewController(vc: ZMHomeViewController(), title: "首页", imageName: "v2_home")
         addChildViewController(vc: ZMCategoryViewController(), title: "分类", imageName: "v2_order")
         addChildViewController(vc: ZMShoppingCarViewController(), title: "购物车", imageName: "shopCart")
         addChildViewController(vc: ZMMineViewController(), title: "我的", imageName: "v2_my")
       
        
    }
    
    
    var lastDidSelectItemIndex : Int = 0
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        var subViewArr = [UIView]()
        for tabBarButton in tabBar.subviews {
            if tabBarButton.isKind(of: NSClassFromString("UITabBarButton")!) {
                subViewArr.append(tabBarButton.subviews.first!)
            }
        }
         
        if let index = tabBar.items?.index(of: item) {
                 let animation = CABasicAnimation(keyPath: "transform.scale")
                animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
                animation.duration = 0.12
                animation.repeatCount = 1
                animation.autoreverses = true
                animation.fromValue = NSNumber(value: 0.5)
                animation.toValue = NSNumber(value: 1.3)
                subViewArr[index].layer.add(animation, forKey: nil)
        }
      
        //记录上一次点击的item
        if let index = tabBar.items?.index(of: item) {
            
            if index > 1 {
                let nav : ZMNavController = self.viewControllers![lastDidSelectItemIndex] as! ZMNavController
        
                
                let control = ZMLoginViewController()
                 
                print("push-before:\( nav.viewControllers.count)")
                nav.pushViewController(control, animated: false)
                print("push-after:\( nav.viewControllers.count)")
                
                NotificationCenter.default.post(name: NSNotification.Name("tabbar"), object: lastDidSelectItemIndex)
                
            
            }
            if index == 0 || index == 1 {
                
                lastDidSelectItemIndex = index
            }
            
        }
    }

    
        
    
    
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {

        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named:"\(imageName)_r")?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 13),NSForegroundColorAttributeName:UIColor.darkGray], for: UIControlState.normal)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 13),NSForegroundColorAttributeName:UIColor.darkGray], for: UIControlState.selected)
        
        vc.view.backgroundColor = UIColor.white
        let nav = ZMNavController(rootViewController: vc)
        addChildViewController(nav)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
